package com.zl.tby.service.impl;

import com.zl.tby.core.common.R;
import com.zl.tby.model.TbyFireRating;
import com.zl.tby.repository.TbyFireRatingRepo;
import com.zl.tby.service.ITbyFireRatingService;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.annotation.Db;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 火险等级配置
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyFireRatingServiceImpl implements ITbyFireRatingService {

    @Db
    private TbyFireRatingRepo tbyFireRatingRepo;

    @Override
    @Tran
    public R<TbyFireRating> add(TbyFireRating entity) {
        entity.setCreateBy(-1L);
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyFireRatingRepo.insert(entity, true);
        entity.setId(id);
        //Pg.updateFireDangerRating(entity.getFireDangerRating());
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyFireRating> update(TbyFireRating entity) {
        entity.setUpdateBy(-1L);
        entity.setUpdateTime(LocalDateTime.now());
        tbyFireRatingRepo.updateById(entity, false);
        //Pg.updateFireDangerRating(entity.getFireDangerRating());
        return R.ok(entity);
    }

    @Override
    public TbyFireRating get() {
        List<TbyFireRating> dbList = tbyFireRatingRepo.selectList(mapperWhereQ -> {
            mapperWhereQ.whereTrue();
        });
        return dbList == null || dbList.size() == 0 ? new TbyFireRating() : dbList.get(0);
    }
}
